<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Http\Controllers\Admin;

use App\Services\WebCatService;
use App\Services\WebSiteService;
use Illuminate\Http\Request;

class WebCatController extends Backend
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->service = new WebCatService();
    }

    public function index()
    {
        $param = request()->input();
        $website=$param['website']??'';
        if(IS_POST){
            $website_search=$param['where']['website']??'';
            $data=[];
            if(!$website_search){
                if($website){
                    $data[]=['website','=',$website];
                }else{
                    $list=(new WebSiteService())->getSiteList();
                    if($list){
                        $website=key($list);
                        $data[]=['website','=',$website];
                    }
                }
            }
            return $this->service->getList(true,$data,[['parent_id','asc'],['listsort','asc'],['id','asc']]);
        }else{
            view()->share('website',$website);
            view()->share('siteList',(new WebSiteService())->getSiteList());
        }

        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function add()
    {
        if(IS_GET){
            $parent_name='顶级菜单';
            $parent_id=intval(request()->input('id',0));
            if($parent_id>0){
                $parentInfo=$this->service->info($parent_id);
                if($parentInfo){
                    $parent_name=$parentInfo['name'];
                }else{
                    $parent_id=0;
                }
            }
            view()->share('website',request()->input('website',''));
            view()->share('parent_id',$parent_id);
            view()->share('parent_name',$parent_name);
            view()->share('typeList',$this->service->typeList());
            view()->share('siteList',(new WebSiteService())->getSiteList());
        }
        return parent::add(); // TODO: Change the autogenerated stub
    }

    public function edit()
    {
        if(IS_GET){
            view()->share('typeList',$this->service->typeList());
            view()->share('siteList',(new WebSiteService())->getSiteList());
        }
        return parent::edit(); // TODO: Change the autogenerated stub
    }

    public function tree(){
        if(IS_POST){
            return $this->service->getTree();
        }else{
            $id = request()->input('id', 0);
            $website = request()->input('website', '');
            $root = request()->input('root', 1);
            view()->share('website',$website);
            view()->share('menuId',$id);
            view()->share('root',$root);
            return $this->render();
        }
    }
}
